package com.doit.day03

import com.doit.day01.Person

import scala.io.Source

object _03_需求1 {
  def main(args: Array[String]): Unit = {
    //将文件中的值都读取出来，放在一个集合或者数组里面面，然后将一个个的句子都转换成单词，放在数组中
    val arr: Array[String] = Source.
      fromFile("data/line.txt")
      .getLines()
      .toArray

    //把数组中的句子全部切开来变成一个个的单词放在数组中
    val flatten: Array[String] = arr.map((str: String) => {
      str.split(",")
    })
      .flatten


    val ints: Array[Int] = Array(1, 2, 3, 4, 5)
    println(ints.count((a: Int) => {
      a==a
    }))//2


    //上强度  ==》 sql  把sql转换成 算子
    //groupBy  ==》 Array("zss","zss","lss","zss","ww","lss","lss")
    val map: Map[String, Array[String]] = flatten.groupBy((word: String) => {
      word
    })


    for (elem <- map) {
      val key: String = elem._1
      val value: Array[String] = elem._2
      println(key+"=>"+value.mkString(","))
    }


    val list: List[Person] = List(
      new Person(1, "zss", 18, "male"),
      new Person(2, "lss", 18, "male"),
      new Person(3, "ww", 18, "female"),
      new Person(4, "zl", 18, "male"),
      new Person(5, "liuyan", 18, "female"),
      new Person(6, "tanyan", 18, "male")
    )

    val intToPersons: Map[Int, List[Person]] = list.groupBy((p: Person) => {
      p.getAge
    })


    //需求：word count
    //读取文件
    //放到集合里面
    //切割并压平 ==> list(单词，单词，单词)
    //groupBy 分组   单词  list(单词，单词，单词)

    Source.fromFile("data/line.txt")
      .getLines()
      .toList
      .flatMap((line:String)=>{line.split("_")})
      .groupBy((word:String)=>word)
      .map(tp=>{
        val key: String = tp._1
        val value: List[String] = tp._2
        key+"==>"+value.size
      })
      .foreach(println)





  }

}
